<?php

namespace App\Kuafu\Mybank\Model;

use App\Kuafu\Base\Model\BaseModel;
use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use App\Kuafu\Base\Model\Traits\KfUid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;

class MybankVostroBillModel extends BaseModel
{

    use HasFactory, KfSoftDeletes, KfUid;

    protected $connection = 'mysql';
    protected $table      = "mybank_vostro_bill";

    protected $fillable = [
        'id',
        'isvOrgId',
        'orderNo',
        'amount',
        'transferDate',
        'flagMark',
        'merchantId',
        'outTradeNo',
        'transNo',
        'channelId',
        'payerBankId',
        'payerCardNo',
        'payerCardName',
        'payeeCardNo',
        'payeeCardName',
        'vostroScene',
        'currency',
        'memo',
        'extInfo',
        'status',
        'remark',
        'vostroType',
        'type',
        'platformFee',
        'feeCurrency',
        'withdrawApplyDate',
        'relateOrderNo',
        'relateTransNo',
        'participantActualRefundAmount',
        'participantType',
        'participantId',
    ];

    const DEFAULT_FLAG_MARK = 0;
    const MATCH_FLAG_MARK   = 1;

    const MAPPING_FLAG_MARK = [
        self::DEFAULT_FLAG_MARK => '未匹配',
        self::MATCH_FLAG_MARK   => '已匹配',
    ];

    const DEFAULT_TYPE         = 0;
    const RECHARGE_TYPE        = 1;
    const WITHDRAW_TYPE        = 2;
    const RECHARGE_REFUND_TYPE = 3;

    const MAPPING_TYPE = [
        self::DEFAULT_TYPE         => '渠道结算入账',
        self::RECHARGE_TYPE        => '充值',
        self::WITHDRAW_TYPE        => '提现',
        self::RECHARGE_REFUND_TYPE => '退款',//提现的另一种 之前充值的退款
    ];

    const MERCHANT_PARTICIPANT_TYPE   = 'MERCHANT';
    const PLATFORM_PARTICIPANT_TYPE   = 'PLATFORM';
    const SUBACCOUNT_PARTICIPANT_TYPE = 'SUBACCOUNT';

    const MAPPING_PARTICIPANT_TYPE = [
        self::MERCHANT_PARTICIPANT_TYPE   => '商户',
        self::PLATFORM_PARTICIPANT_TYPE   => '平台',
        self::SUBACCOUNT_PARTICIPANT_TYPE => '保证金账号',
    ];

    /**
     * @return HasOne
     */
    public function mybankEntryRel(): HasOne
    {
        return $this->hasOne(MybankEntryModel::class, "merchantId", "merchantId");
    }

    public function mybankOrderWithholdRecordRel()
    {
        return $this->hasOneThrough(
            MybankOrderWithholdRecordModel::class,
            MybankBillRecordBindModel::class,
            'vostroBillId',
            'batchNo',
            'id',
            'batchNo'
        );
    }

    public static function totalAmount(): int
    {
        return self::query()
                   ->where('type', self::DEFAULT_TYPE)//渠道结算入账
                   ->where('flagMark', self::DEFAULT_FLAG_MARK)
                   ->sum('amount');
    }

    public static function addData(array $data)
    {
        return self::query()->create($data);
    }
}
